package com.usian.paymentdemo.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.FileItem;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("alipay")
public class AlipayTradePagePay {

    @RequestMapping("/pay")
    public String pay() throws AlipayApiException {
        {
            String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCbfyVsiO83cesTtQsMNqnm1tKP9jT1rFFbGfmwq7C/h7GiEkgMaFLC1BTqftDLGm6inFk2Pmm2OyZUY/sEIAjh6WTJF7q0lz+yQX2wKsbT6HhFiZs7UHkTCQCJLwHZGC+CjsQuaAHEa9sgrmovlV2P86lKu6/IBvxjKpWQN+rdHPOTSgKCpmQZ164JbUF7PbRvEYGzEiwauNIu7suPtxAZdDS+Dw2BSSPcubIP38DNAq8lbngJkRRo+ZoYTYOQAVr++5HjAsHTqk0pHcz5tBuGdO6CrV9JvdkKXtzav5E9Z5jba7+7obKT1Wl3TpYWGxlDNi7zh7eRcm06qZA3Xxi3AgMBAAECggEAWRVssVN6CP/At5ee7q5ahB9KnqNlD+Ja8aBeBjM2nWP2Bm4GzOTvlSK/oOBqahGIlKySB5uWIIqemPZ2lpRrwlFeJj7r/g4wtcUDZuv64OhsDxqYzlB6LMeZgP5nRqKuhVOZXSTE7m0EKyMBPqWJ5rCnqmMAf85QxdddvDPs4CORbkknXJU4ua9q/jqT6iaouDeo/r9fn1suO4b8i1/KorlArAItDkbxCX3aYAU+gFVhDNKbc8m10RBnTwscp4Ym+D2UwT6c2JejHvwSpqvyYWZR0XETw2UIrHgQBfREoUT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vAQqd+1iOEVc/s1S6i/a/P2Jvqf2vet87jlbsBv2I7TrjexxF9rVNGhraJKQr9gfH8W6jzsJDTVyiz9MtxGXYcYoR077zC/aIZBXrft3xG+ZyARHo2fu4HaU+q//OkQP9WvtWDulr2Q3oJPjs2k8cnJFAo3m/D/FOCSinH96G4Y5KwXxBMnzh1ne+EF6ZftY6DQgTjgxuY8Lf3imcXXzjy8oFGYxUxC68zURKQZExSiGDPXKFOpCytluZ8kwIDAQAB";
            AlipayConfig alipayConfig = new AlipayConfig();
            alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
            alipayConfig.setAppId("9021000132690996");
            alipayConfig.setPrivateKey(privateKey);
            alipayConfig.setFormat("json");
            alipayConfig.setAlipayPublicKey(alipayPublicKey);
            alipayConfig.setCharset("UTF-8");
            alipayConfig.setSignType("RSA2");
            AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
            AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            AlipayTradePagePayModel model = new AlipayTradePagePayModel();
            model.setOutTradeNo("20150320010101001");
            model.setTotalAmount("88.88");
            model.setSubject("Iphone6 16G");
            model.setProductCode("FAST_INSTANT_TRADE_PAY");
            request.setBizModel(model);
            AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
            System.out.println(response.getBody());
            return response.getBody();
//            if (response.isSuccess()) {
//                System.out.println("调用成功");
//            } else {
//                System.out.println("调用失败");
//                // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
//                // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
//                // System.out.println(diagnosisUrl);
//            }
        }
    }

    public static void main(String[] args) throws AlipayApiException {
        String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCbfyVsiO83cesTtQsMNqnm1tKP9jT1rFFbGfmwq7C/h7GiEkgMaFLC1BTqftDLGm6inFk2Pmm2OyZUY/sEIAjh6WTJF7q0lz+yQX2wKsbT6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+0wB712Yg0vG1yFxxCa1VGUF1HoR9kFjwbIV1ZQKBgBy6cQE7SHAbKh7Lm6BBPWwEEkgvXqcIUX/BC5/agyd8FTcqEnhz6BJ+qGLdIUvI51QcmEal2tEvURVahD+xkbdK/ueapsrwQFNwmpiK9/AXaFfvuZBELEPHFnh45xVHT4RQpVOMGce+p3otyTsgbQ5zbOcFP9wzIqgZ4lFYfpLBAoGAQAKiOCg2zfjwgcTuTf85FzcdZhqbJ9ZFBU+gnXPbpfPBUEV9Sn7hXiaFYQYkQD5X82uZWz0ctwgIY6cGJsZnx7UvJLIzBw1jwJ76KqiBbyjfhgMxRuQDvJ3N8JNE7ydtarH1x6250M3uZU6jEANXzt+7SzxYwh9r1nz8/IPg2Vc=";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqsVebyF+31stH4xkMeTPpwszLtPMMWw1dWfKLEWiC7khwGFaW+rEjoIdL8es/YJGTr2JdybmIPbqMtUwty33kCTJx2vAQqd+1iOEVc/s1S6i/a/P2Jvqf2vet87jlbsBv2I7TrjexxF9rVNGhraJKQr9gfH8W6jzsJDTVyiz9MtxGXYcYoR077zC/aIZBXrft3xG+ZyARHo2fu4HaU+q//OkQP9WvtWDulr2Q3oJPjs2k8cnJFAo3m/D/FOCSinH96G4Y5KwXxBMnzh1ne+EF6ZftY6DQgTjgxuY8Lf3imcXXzjy8oFGYxUxC68zURKQZExSiGDPXKFOpCytluZ8kwIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000132690996");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");

        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setOutTradeNo("20150320010101001");
        model.setTotalAmount("88.88");
        model.setSubject("Iphone6 16G");
        model.setProductCode("FAST_INSTANT_TRADE_PAY");

        request.setBizModel(model);
        request.setNotifyUrl("");
        request.setReturnUrl("");
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }
}